package GUI;

import Ctr.CustomerCtr;
import Ctr.ProductCtr;
import Ctr.SalesOrderCtr;
import Ctr.SupplierCtr;
import Model.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

public class MainGUI {

    private JPanel mainWindow;
    private JFrame frame;
    private JTabbedPane tbpMain;
    private JPanel tabCustomer;
    private JPanel tabOrder;
    private JPanel tabProducts;
    private JPanel tabSupplier;
    private JTextField txtSupplierName;
    private JTextField txtSupplierAddress;
    private JTextField txtSupplierCountry;
    private JTextField txtSupplierPhoneNo;
    private JTextField txtSupplierEmail;
    private JButton btnAddNewSupplier;
    private JLabel lblSupplierName;
    private JLabel lblSupplierAddress;
    private JLabel lblSupplierCountry;
    private JLabel lblSupplierPhoneNo;
    private JLabel lblSupplierEmail;
    private JPanel pnlSupplierInfo1;
    private JPanel pnlSupplierInfo2;
    private JTable tblSupplierInfo;
    private JButton btnDeleteSupplier;
    private JTextField txtSupplierIdDelete;
    private JLabel lblSupplierIdDelete;
    private JPanel pnlNewSupplier;
    private JPanel pnlSupplierList;
    private JPanel pnlNewProduct;
    private JPanel pnlProductList;
    private JPanel pnlNewCustomer;
    private JPanel pnlCustomerList;
    private JPanel pnlCustomerInfo1;
    private JPanel pnlCustomerInfo2;
    private JTextField txtCustomerName;
    private JTextField txtCustomerAddress;
    private JTextField txtCustomerZipCode;
    private JTextField txtCustomerPhoneNo;
    private JButton btnAddNewCustomer;
    private JLabel lblCustomerName;
    private JLabel lblCustomerAddress;
    private JLabel lblCustomerZipCode;
    private JLabel lblCustomerPhoneNo;
    private JTable tblCustomers;
    private JTextField txtCustomerDeleteID;
    private JButton btnCustomerDelete;
    private JLabel lblCustomerDeleteId;
    private JTextField txtProductName;
    private JTextField txtProductCountry;
    private JTextField txtProductMinStock;
    private JTextField txtProductPurchasePrice;
    private JTextField txtProductSalesPrice;
    private JTextField txtProductRentPrice;
    private JComboBox cmbProductType;
    private JComboBox cmbProductSupplier;
    private JButton btnAddProduct;
    private JLabel lblProductName;
    private JLabel lblProductCountry;
    private JLabel lblProductMinStock;
    private JLabel lblProductPurchasePrice;
    private JLabel lblProductSalesPrice;
    private JLabel lblProductRentPrice;
    private JLabel lblProductType;
    private JLabel lblProductSupplier;
    private JTable tblProducts;
    private JButton btnDeleteProduct;
    private JTextField txtProductDeleteID;
    private JLabel lblProductDeleteID;
    private JPanel pnlProductInfo1;
    private JPanel pnlProductInfo2;
    private JButton btnOrderConfirmOrder;
    private JButton btnOrderAddItem;
    private JComboBox cmbCustomer;
    private JLabel lblOrderCustomer;
    private JTable tblOrderNewOrder;
    private JPanel pnlOrderNewOrder;
    private JPanel pnlOrderOrders;
    private JTable tblOrderSaleOrders;
    private JButton button1;
    private JTextField txtOrderDate;
    private JTextField txtOrderDelStatus;
    private JTextField txtOrderDelDate;
    private JLabel lblOrderDate;
    private JTextField txtOrderTotalPrice;
    private JLabel lblOrderTotalPrice;

    private SupplierCtr supplierCtr;
    private CustomerCtr customerCtr;
    private ProductCtr productCtr;
    private SalesOrderCtr salesOrderCtr;


    public MainGUI() {

        supplierCtr = new SupplierCtr();
        customerCtr = new CustomerCtr();
        productCtr = new ProductCtr();
        salesOrderCtr = new SalesOrderCtr();

        createSupplierTable();
        createCustomerTable();
        createProductTable();
        createOrderTable();
        createComboBoxes();
        createCustomerComboBox();

        //Add a new supplier
        btnAddNewSupplier.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    insertSupplier();
                }
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "Phone number not valid");
                }
                createSupplierTable();
                clearSupplierInfo();
                createComboBoxes();
            }
        });

        //Delete supplier
        btnDeleteSupplier.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    deleteSupplier();
                }
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "Not a valid supplier ID");
                }
                createSupplierTable();
                createComboBoxes();

            }
        });

        //Insert customer
        btnAddNewCustomer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                insertCustomer();
                createCustomerTable();
                createCustomerComboBox();
            }
        });

        //Delete customer
        btnCustomerDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    deleteCustomer();
                }
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "Not a valid customer ID");
                }
                createCustomerTable();
                createCustomerComboBox();
                txtCustomerDeleteID.setText("");
            }
        });

        //Insert product
        btnAddProduct.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    insertProduct();
                }
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "The input in min. stock, purchase price, rent price and sales price has to be a number");
                }

                createProductTable();
            }
        });

        //Delete product
        btnDeleteProduct.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    deleteProduct();
                }
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "Not a valid product ID");
                }
                createProductTable();
                txtProductDeleteID.setText("");
            }
        });
        btnOrderAddItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ProductSearch search = new ProductSearch(salesOrderCtr);
                search.setVisible(true);
            }
        });

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createNewOrderTable();
                refreshTotalPrice();
            }
        });

        btnOrderConfirmOrder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectCustomer();
                completeSalesOrder();
                createOrderTable();
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Western Styles Ltd.");
        frame.setContentPane(new MainGUI().mainWindow);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(new Dimension(800, 600));
        frame.setResizable(false);
        frame.setVisible(true);
    }


    //SUPPLIER
    //Inserting a supplier
    private void insertSupplier() {
        supplierCtr.insertSupplier(supplierCtr.getMaxId(), txtSupplierName.getText(), txtSupplierAddress.getText(),
                txtSupplierCountry.getText(), Integer.parseInt(txtSupplierPhoneNo.getText()), txtSupplierEmail.getText());

    }

    //Deleting a supplier
    private void deleteSupplier() {
        supplierCtr.deleteSupplier(Integer.parseInt(txtSupplierIdDelete.getText()));
    }

    //Creating the supplier table
    private void createSupplierTable() {
        ArrayList<Supplier> supplierArrayList = supplierCtr.getAllSuppliers(false);
        SupplierTableModel model = new SupplierTableModel();
        model.setData(supplierArrayList);
        tblSupplierInfo.setModel(model);
    }

    //Clear supplier info
    private void clearSupplierInfo() {
        txtSupplierName.setText("");
        txtSupplierAddress.setText("");
        txtSupplierCountry.setText("");
        txtSupplierPhoneNo.setText("");
        txtSupplierEmail.setText("");
    }


    //CUSTOMER
    //Inserting a customer
    private void insertCustomer() {
        customerCtr.insertCustomer(customerCtr.getMaxId(), txtCustomerName.getText(), txtCustomerAddress.getText(),
                Integer.parseInt(txtCustomerZipCode.getText()), Integer.parseInt(txtCustomerPhoneNo.getText()));
    }

    //Delete customer
    private void deleteCustomer() {
        customerCtr.deleteCustomer(Integer.parseInt(txtCustomerDeleteID.getText()));

    }

    //Create customer table
    private void createCustomerTable() {
        ArrayList<Customer> customers = customerCtr.getAllCustomers(false);
        CustomerTableModel model = new CustomerTableModel();
        model.setData(customers);
        tblCustomers.setModel(model);
    }

    //PRODUCT
    //Insert product
    private void insertProduct() {
        productCtr.insertProduct(productCtr.getMaxId(), txtProductName.getText(), Float.parseFloat(txtProductPurchasePrice.getText()), Float.parseFloat(txtProductSalesPrice.getText()), Float.parseFloat(txtProductRentPrice.getText()), txtProductCountry.getText(), Integer.parseInt(txtProductMinStock.getText()), cmbProductType.getSelectedItem().toString(), Integer.parseInt(cmbProductSupplier.getSelectedItem().toString()));
    }

    //Initializing comboboxes
    private void createComboBoxes() {
        ArrayList<Supplier> list;

        list = supplierCtr.getAllSuppliers(false);

        cmbProductSupplier.removeAllItems();
        cmbProductType.removeAllItems();

        for (Supplier s : list) {
            cmbProductSupplier.addItem(s.getId());
        }

        cmbProductType.addItem("Clothing");
        cmbProductType.addItem("Equipment");
        cmbProductType.addItem("Gun replicas");
    }

    private void createCustomerComboBox() {
        ArrayList<Customer> list;

        list = customerCtr.getAllCustomers(false);

        cmbCustomer.removeAllItems();

        for (Customer c : list) {
            cmbCustomer.addItem(c.getId());
        }
    }

    //Delete product
    private void deleteProduct() {
        productCtr.deleteProduct(Integer.parseInt(txtProductDeleteID.getText()));
    }

    //Create product table
    private void createProductTable() {
        ArrayList<Product> products = productCtr.getAllProducts(false);
        ProductTableModel model = new ProductTableModel();
        model.setData(products);
        tblProducts.setModel(model);
    }

    //SALESORDER
    //Create order table
    private void createOrderTable() {
        ArrayList<SalesOrder> orders = salesOrderCtr.getAllSalesOrders();
        OrderTableModel model = new OrderTableModel();
        model.setData(orders);
        tblOrderSaleOrders.setModel(model);
    }

    //Create newOrder table
    public void createNewOrderTable() {
        ArrayList<SalesOrderProduct> salesOrderProducts = salesOrderCtr.getAllSalesOrderProducts();
        NewOrderTableModel model = new NewOrderTableModel();
        model.setData(salesOrderProducts);
        tblOrderNewOrder.setModel(model);
    }

    //Select customer
    private void selectCustomer() {
        salesOrderCtr.selectCustomer(Integer.parseInt(cmbCustomer.getSelectedItem().toString()));
    }

    private void completeSalesOrder() {
        salesOrderCtr.createSalesOrder(salesOrderCtr.getMaxId(), txtOrderDate.getText(), salesOrderCtr.getTotalPrice(), txtOrderDelStatus.getText(), txtOrderDelStatus.getText());
    }

    private void refreshTotalPrice() {
        txtOrderTotalPrice.setText(String.valueOf(salesOrderCtr.getTotalPrice()));
    }
}
